package com.xdkj.web.servlet;

import com.xdkj.commons.util.BaseResponse;
import com.xdkj.commons.util.ResponseEnum;
import com.xdkj.model.dao.TJobTitleDao;
import com.xdkj.model.dao.impl.TJobTitleDaoImpl;
import com.xdkj.model.entity.TJobtitle;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "JobtitleServlet", urlPatterns = "/jobtitle")
public class JobtitleServlet extends BaseServlet {
    private TJobTitleDao jobTitleDao = new TJobTitleDaoImpl();

    /**
     * 查询所有职称
     *
     * @param request  请求
     * @param response 响应
     * @return {@link BaseResponse}
     * @throws ServletException servlet异常
     * @throws IOException      ioexception
     */
    public BaseResponse queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        BaseResponse baseResponse = null;
        List<TJobtitle> list = jobTitleDao.queryAll();
        TJobtitle jobTitle = jobTitleDao.queryByName(name);
        Map<String, Object> map = new HashMap<>();
        if (list.size() > 0) {
            map.put("list", list);
            map.put("jobtitle", jobTitle);
            baseResponse = BaseResponse.ok(ResponseEnum.QUERYSUCCESS.getCode(), ResponseEnum.QUERYSUCCESS.getMessage(), map);
        } else {
            baseResponse = BaseResponse.error(ResponseEnum.QUERYFAILURE.getCode(), ResponseEnum.QUERYFAILURE.getMessage());
        }
        return baseResponse;
    }
}
